import 'dart:async';
import 'dart:math';
import 'dart:ui';

import 'package:flutter/material.dart';
import 'package:slide_popup_dialog/slide_popup_dialog.dart' as slideDialog;
import 'package:cached_network_image/cached_network_image.dart';

//main(List<String> args) {
//  runApp(Basewhidget());
//}

class Basewhidget extends StatelessWidget {
  // statelessWidget 是一个无状态的组件，用于自定义组件

  @override
  Widget build(BuildContext context) {
    return MaterialApp(

      // materialAPP 是作为根组件使用，定义该页面展示的总体布局
        home: Home()
    );
  }

}

class Home extends StatefulWidget {
  @override
  _HomeState createState() => _HomeState();
}

class _HomeState extends State<Home> {
  ScrollController _scrollController = new ScrollController(initialScrollOffset: 0);
  @override
  Widget build(BuildContext context) {
    return Scaffold(
      appBar: AppBar(
        title: Text("滑动控制"),
      ),
      body: MyHomePage(),
//      回到最上页
//      floatingActionButton: !true
//          ? null
//          : FloatingActionButton(
//        onPressed: () {
//          _scrollController.jumpTo(10);
//        },
//        child: Icon(Icons.arrow_upward),
//      ),

    );
  }
}


class MyHomePage extends StatefulWidget {
  final String title;

  const MyHomePage({Key key, this.title}) : super(key: key);

  @override
  Scontroller1 createState() => Scontroller1();
}



class Scontroller1 extends State<MyHomePage> {
//   获取控制器，用于监听滑动事件
  ScrollController _scrollController = new ScrollController(initialScrollOffset: 0);
  bool isShow = false;
  String _progress = "0%";
  TextStyle isshow = TextStyle(color: Colors.white);
  int Nums = 4;
  CachedNetworkImage ImageWiget;
  bool on_off = false;
  List<List<Widget>> ImageList = [[   // 这里后期可以进行优化
    CachedNetworkImage(imageUrl: 'http://api.btstu.cn/sjbz/?lx=m_dongman&a=1&b=0',fit: BoxFit.cover,),
    CachedNetworkImage(imageUrl: 'http://api.btstu.cn/sjbz/?lx=m_dongman&a=2&b=0',fit: BoxFit.cover,),
    CachedNetworkImage(imageUrl: 'http://api.btstu.cn/sjbz/?lx=m_dongman&a=3&b=0',fit: BoxFit.cover,),
  ],[
    CachedNetworkImage(imageUrl: 'http://api.btstu.cn/sjbz/?lx=m_dongman&a=1&b=1', fit: BoxFit.cover,),
    CachedNetworkImage(imageUrl: 'http://api.btstu.cn/sjbz/?lx=m_dongman&a=2&b=1',fit: BoxFit.cover,),
    CachedNetworkImage(imageUrl: 'http://api.btstu.cn/sjbz/?lx=m_dongman&a=3&b=1',fit: BoxFit.cover,),
  ],
    [
      CachedNetworkImage(imageUrl: 'http://api.btstu.cn/sjbz/?lx=m_dongman&a=1&b=2',fit: BoxFit.cover,),
      CachedNetworkImage(imageUrl: 'http://api.btstu.cn/sjbz/?lx=m_dongman&a=2&b=2',fit: BoxFit.cover,),
      CachedNetworkImage(imageUrl: 'http://api.btstu.cn/sjbz/?lx=m_dongman&a=3&b=2',fit: BoxFit.cover,),
    ], [
      CachedNetworkImage(imageUrl: 'http://api.btstu.cn/sjbz/?lx=m_dongman&a=1&b=3',fit: BoxFit.cover,),
      CachedNetworkImage(imageUrl: 'http://api.btstu.cn/sjbz/?lx=m_dongman&a=2&b=3',fit: BoxFit.cover,),
      CachedNetworkImage(imageUrl: 'http://api.btstu.cn/sjbz/?lx=m_dongman&a=3&b=3',fit: BoxFit.cover,),
    ]
  ];

  @override
  void initState() {
    // TODO: implement initState
    super.initState();
    _scrollController.addListener(() {
      int offset = _scrollController.position.viewportDimension.toInt();
      print("滑动距离$offset");
      print("num 的数量是： ${this.Nums}， Imagelist 数量是： ${ImageList.length}");

      // 如果滑动到底部
      if (_scrollController.offset ==
          _scrollController.position.maxScrollExtent) {
        setState(() {
          print("滑到底部了");
          setState(() {
            List aaa = [0, 1, 2]; // 循环条件
            List<List<Widget>> lists = [];
            for(dynamic x in aaa)
            {
              this.Nums += 1;   // 动态添加展示数据
              List<Widget> list = [];
              for(dynamic b in aaa){
                list.add(CachedNetworkImage(imageUrl: 'http://api.btstu.cn/sjbz/?lx=m_dongman&a=${b}&b=${this.Nums}',fit: BoxFit.cover),);
              }
              lists.add(list);
            }
            ImageList.addAll(lists);
            print("num 的数量是： ${this.Nums}， Imagelist 数量是： ${ImageList.length}");

          });

          isShow = true;
        });
      } else if(_scrollController.offset <= 0){
        print("顶部下拉, 下拉的大小是： ${this._scrollController.offset}");
        if (this._scrollController.offset <= -50) {
          print("松开即可刷新");
          setState(() {
            isshow = TextStyle(color: Colors.black12);
          });
          this.on_off = true;

        }
        else if(this.on_off == true && _scrollController.offset >= 0){
          this.ImageList.clear();
          var rng = new Random();

          List aaa = [rng.nextInt(10000000), rng.nextInt(10000000), rng.nextInt(10000000), rng.nextInt(1000000)]; // 循环条件
          print(rng.nextInt(10000000));
          print("随机数列表是： ${aaa}, 当前的开关是:  ${this.on_off}. 当前的滑动位置是： ${_scrollController.offset}");
          this.Nums = 0;
          on_off = false;
          setState(() {
            isshow = TextStyle(color: Colors.white);

            List<List<Widget>> lists = [];
            for(dynamic x in aaa)
            {

              List<Widget> list = [];
              for(dynamic b in aaa){
                list.add(CachedNetworkImage(imageUrl: 'http://api.btstu.cn/sjbz/?lx=m_dongman&a=${b}&b=${this.Nums}',fit: BoxFit.cover),);
              }
              lists.add(list);
              this.Nums += 1;   // 动态添加展示数据
            }
            ImageList.addAll(lists);
            print("num 的数量是： ${this.Nums}， Imagelist 数量是： ${ImageList.length}");
          });
        }
      }
      else {
        setState(() {
          isShow = false;
        });
      }
    });
  }

  @override
  void dispose() {
    // TODO: implement dispose
    _scrollController.dispose();
    super.dispose();
  }

  @override
  Widget build(BuildContext context) {
    // TODO: implement build
    return Container(


      padding: EdgeInsets.all(5),

      child: Stack(
        alignment: Alignment.center,
        children: <Widget>[
          Container(
            height: 800,
            child: Text("松开即可刷新", style: this.isshow,),
//            color: Colors.black12,
          ),
          Scrollbar(
            child: NotificationListener(
              // ignore: missing_return
                onNotification: (ScrollNotification notification) {
                  //滑动进度/最大可滑动进度
                  double progress = notification.metrics.pixels /
                      notification.metrics.maxScrollExtent;
                  setState(() {
                    _progress = "${((progress * 100).toInt())}%";
                  });
                },
                child: ListView.separated(

                    physics: BouncingScrollPhysics(),
                    controller: _scrollController,
                    itemBuilder: (BuildContext buildContext, int index) {
                      print("index是 $index, Nmus 是$Nums");
                      return Container(
                        margin: EdgeInsets.fromLTRB(5, 5, 3, 0),
                        child: Row(
                          children: <Widget>[
                            Expanded(

                                child: InkWell(
                                  onTap: () {
                                    print("第一个图片被点击了, }");
//                                    Navigator.pushNamed(context, "/click");    //在arguments 中传递数据
                                    Navigator.pushNamed(context, "/click", arguments: {"image": this.ImageList[index][0]});    //在arguments 中传递数据

                                  },
                                  child: Container(
//                                  color: Colors.yellow,
                                    height: 200,
                                    margin: EdgeInsets.fromLTRB(0, 0, 0, 5),

                                    child: this.ImageList[index][0],
                                  ),
                                )

                            ),
                            Expanded(
                                child: InkWell(
                                  onTap: () {
                                    print("第二个图片被点击了");
                                    setState(() {
                                        ImageWiget = this.ImageList[index][0];
                                    });
                                  },
                                  child: Container(
//                                  color: Colors.blue,
                                    margin: EdgeInsets.fromLTRB(8, 0, 5, 5),
                                    height: 200,
                                    child: ImageList[index][1],
                                  ),
                                )

                            ),
                            Expanded(
                                child: InkWell(
                                  onTap: () {
                                    print("第三个图片被点击了");
                                    setState(() {
                                      ImageWiget = this.ImageList[index][0];
                                    });
                                  },
                                  child: Container(
                                    height: 200,
                                    margin: EdgeInsets.fromLTRB(3, 0, 5, 5),
                                    child: ImageList[index][2],
                                  ),
                                )

                            ),
                          ],
                        ),
                      );
                    },
                    separatorBuilder: (BuildContext buildContext, int index) {
                      return Divider(
                        color: Colors.white,
                        height: 0,
                      );
                    },
                    itemCount: this.Nums)),  // 这里是滑动数量


          ),

//            CircleAvatar(
//              //显示进度百分比
//              radius: 50.0,
//              child: Text("松开即可刷新图片"),
//              backgroundColor: Colors.black54,
//
//            ),
        ],
      ),
    );

  }
}

